package com.briup.smartlabs.config;

import java.io.OutputStream;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

//@Configuration	//加这个注解的类代表是Spring的配置类。
public class CommPortConfig { 
	@Bean("spOut")
	public OutputStream getOutputStream() throws Exception {
		SerialPort sp = openSerialPort();
		return sp.getOutputStream();
	}
	
	@Bean("serialPort")	//加了这个注解以后，方法的返回值会自动放入spring容器。
	public SerialPort openSerialPort() throws Exception{
		CommPortIdentifier cpi = 
				CommPortIdentifier.getPortIdentifier("COM3");
		CommPort cp = cpi.open("SmartLabsApp", 2000);
		System.out.println("占用端口COM3");
		SerialPort sp = (SerialPort)cp;
		sp.setSerialPortParams(57600, SerialPort.DATABITS_8, 
				SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		System.out.println("打开串口成功！波特率57600");
		return sp;
	}
}





